package com.tuling.jucdemo.sync;
/**
 * sleep不会释放锁    wait会释放锁
 */
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SyncWaitTest {
    private Object lock = new Object();

    public void test() {
        log.debug(Thread.currentThread().getName() + " start");
        synchronized (lock) {
            log.debug(Thread.currentThread().getName() + " execute");
            try {
                //Thread.sleep(5000);//不会释放锁 必须一个线程结束 另外一个线程才可以执行
                lock.wait(5000);// 会释放锁 两个或多个线程可以同时执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug(Thread.currentThread().getName()+" end");
        }
    }

    public static void main(String[] args) {
        SyncWaitTest syncWaitTest = new SyncWaitTest();
        for (int i = 0; i <2 ; i++) {
            new Thread(new Runnable(){
                @Override
                public void run() {
                    syncWaitTest.test();
                }
            },"thread"+i).start();
        }


    }
}
